package com.itcam.lease.web.app.custom.config;


import com.itcam.lease.web.app.custom.interceptor.AuthenticationInterceptor;
import com.itcam.lease.web.app.custom.interceptor.AuthorizationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration // 表明这是一个配置类
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Value("${app.auth.path-patterns.include}") // 从配置文件中读取需要拦截的路径模式
    private String[] includePathPatterns;

    @Value("${app.auth.path-patterns.exclude}") // 从配置文件中读取不需要拦截的路径模式
    private String[] excludePathPatterns;

    @Autowired // 自动注入 AuthenticationInterceptor 实例
    private AuthenticationInterceptor authenticationInterceptor;

    @Autowired // 自动注入 AuthorizationInterceptor 实例
    private AuthorizationInterceptor authorizationInterceptor;

    /**
     * 添加拦截器到 Spring MVC 的拦截器链中
     *
     * @param registry 拦截器注册器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加认证拦截器，并指定需要拦截和排除的路径模式
        registry.addInterceptor(this.authenticationInterceptor).addPathPatterns(includePathPatterns).excludePathPatterns(excludePathPatterns);
        // 添加授权拦截器，并指定需要拦截和排除的路径模式
        registry.addInterceptor(this.authorizationInterceptor).addPathPatterns(includePathPatterns).excludePathPatterns(excludePathPatterns);
    }
}
